package com.jinxin.platform.cdfaceacs.support;

import com.alibaba.fastjson.JSON;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordRecord;
import com.jinxin.platform.cdfaceacs.pojo.dto.RuleConfig;
import com.jinxin.platform.cdfaceacs.pojo.dto.RuleDto;
import com.jinxin.platform.cdfaceacs.redis.RedisUtil;
import com.jinxin.platform.cdfaceacs.service.CdattendancerecordRecordService;
import com.jinxin.platform.cdfaceacs.utils.MyConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Date: 2021-04-27
 * Author: yangjie
 * desc:
 **/
@Slf4j
@Configuration
@EnableScheduling
public class ScheduleTask {
    @Resource
    CdattendancerecordRecordService recordService;
    @Resource
    RedisUtil redisUtil;

    /**
     * 处理考勤结果
     */
    @Scheduled(cron = "0 0/30 * * * ? ")
//    @Scheduled(cron = "*/20 * * * * ?")
    private void configureTasks() throws ParseException {
        RuleConfig ruleConfig = getRuleDto();

        List<CdattendancerecordRecord> todayRecordList = recordService.getTodayRecord();
        if (todayRecordList == null || todayRecordList.size() == 0) {
            return;
        }
        Date signInDate = ruleConfig.getSignInDate();
        Date signOutDate = ruleConfig.getSignOutDate();
        Date lateDate = ruleConfig.getLateMinutes();
        Date leaveEarlyDate = ruleConfig.getLeaveEarlyMinutes();
        Date absenteeismDate = ruleConfig.getAbsenteeismMinutes();
        todayRecordList.forEach(item -> {
            log.info("考勤记录{}", item.toString());
            Date firstDate = item.getFirstDate();
            Date lastDate = item.getLastDate();
            CdattendancerecordRecord record = new CdattendancerecordRecord();
            record.setId(item.getId());
            if(firstDate == null || lastDate == null){
                item.setStatus("旷工");
            }else {
                // 签到时间晚于迟到时间并且签退时间晚于早退时间视作迟到
                if (firstDate.after(lateDate) && (lastDate.after(leaveEarlyDate) || lastDate.equals(leaveEarlyDate))) {
                    record.setStatus("迟到");
                    // 签退时间早于早退时间并且签到时间早于迟到时间视作早退
                }else if (lastDate.before(leaveEarlyDate) && (firstDate.before(lateDate) || firstDate.equals(lateDate))) {
                    record.setStatus("早退");
                    // 签到时间早于迟到时间并且签退时间晚于早退时间视作正常
                }else if ((lastDate.after(signOutDate) || lastDate.equals(signOutDate)) && (firstDate.before(lateDate) || firstDate.equals(lateDate))) {
                    record.setStatus("正常");
                }else {//其余情况都是旷工
                    record.setStatus("旷工");
                }
            }
            recordService.update(record);
        });
    }

    public static void main(String[] args) throws ParseException {
        Date today = new Date();
        String time = "13:31:9";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String format = formatter.format(today);
        Date date = formatter.parse(format.substring(0, 10) + " " + time);
        System.out.println(date);
        //
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, 10);
        System.out.println(cal.getTime());
    }

    private RuleConfig getRuleDto() throws ParseException {
        Date today = new Date();
        RuleConfig config = new RuleConfig();
        RuleDto ruleDto = JSON.parseObject((String)redisUtil.get(MyConstants.REDIS_ATTENDANCE_CONFIG_OBJ), RuleDto.class);
        //
//        RuleDto ruleDto = new RuleDto();
//        ruleDto.setSignInDate("9:00:00");
//        ruleDto.setSignOutDate("18:00:00");
//        ruleDto.setAbsenteeismMinutes(5);
//        ruleDto.setLateMinutes(5);
//        ruleDto.setLeaveEarlyMinutes(15);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String format = formatter.format(today);
        //签到
        Date signInDate = formatter.parse(format.substring(0, 10) + " " + ruleDto.getSignInDate());
        config.setSignInDate(signInDate);
        //签退
        Date signOutDate = formatter.parse(format.substring(0, 10) + " " + ruleDto.getSignOutDate());
        config.setSignOutDate(signOutDate);
        //迟到
        Calendar cal = Calendar.getInstance();
        cal.setTime(signInDate);
        cal.add(Calendar.MINUTE, ruleDto.getLateMinutes());
        config.setLateMinutes(cal.getTime());
        //早退
        cal.setTime(signOutDate);
        cal.add(Calendar.MINUTE, -ruleDto.getLateMinutes());
        config.setLeaveEarlyMinutes(cal.getTime());
        //旷工
        cal.setTime(signOutDate);
        cal.add(Calendar.MINUTE, -ruleDto.getLateMinutes());
        config.setAbsenteeismMinutes(cal.getTime());
        return config;
    }
}
